package com.payment.ktb.activity.main1;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/8/16.
 */
public class CustomServiceCenterActivity extends BaseActivity {


    @BindView(R.id.tv_customservicecenter_orgNick)
    TextView tv_customservicecenter_orgNick;

    @BindView(R.id.tv_customservicecenter_cshName)
    TextView tv_customservicecenter_cshName;

    @BindView(R.id.tv_customservicecenter_cshPhone)
    TextView tv_customservicecenter_cshPhone;

    @BindView(R.id.tv_customservicecenter_cshTel)
    TextView tv_customservicecenter_cshTel;

    @BindView(R.id.tv_customservicecenter_cshWeixin)
    TextView tv_customservicecenter_cshWeixin;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customservicecenter);
        ButterKnife.bind(this);
        initTitle("客服中心");

        getShopOrgInfo();

    }


    @OnClick({R.id.tv_customservicecenter_cshPhone,R.id.tv_customservicecenter_cshTel})
    public void clickEvent(View view) {
        switch (view.getId()) {

            //客服电话
            case R.id.tv_customservicecenter_cshPhone:
                if (!TextUtils.isEmpty(tv_customservicecenter_cshPhone.getText().toString())) {
                    tel(tv_customservicecenter_cshPhone.getText().toString());
                }
                break;
            //客服座机
            case R.id.tv_customservicecenter_cshTel:
                if (!TextUtils.isEmpty(tv_customservicecenter_cshTel.getText().toString())) {
                    tel(tv_customservicecenter_cshTel.getText().toString());
                }
                break;
        }
    }


    /**
     * 打电话
     */
    public void tel(final String telNo) {
        AlertDialogUtils.showAlertDialog(mContext, telNo, "取消", "呼叫", new AlertInterface() {
            @Override
            public void confirm(Dialog alertDialog) {
                startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + telNo)));
            }
        });
    }

    /**
     * 获取商户帮助信息
     */
    public void getShopOrgInfo() {
        progressDialogUtils.showProgress();

        //构造传输参数
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        String sign = Md5Utils.md5Hex(SharedPreferencesUtils.getString(ConstantsUser.userKey));

        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopOrgInfo, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {

                        tv_customservicecenter_orgNick.setText(jsonObject.optString("orgNick"));
                        tv_customservicecenter_cshName.setText(jsonObject.optString("cshName"));
                        tv_customservicecenter_cshPhone.setText(jsonObject.optString("cshPhone"));
                        tv_customservicecenter_cshTel.setText(jsonObject.optString("cshTel"));
                        tv_customservicecenter_cshWeixin.setText(jsonObject.optString("cshWeixin"));



                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });
    }
}
